/**
 * @name        PreferenceOption.ts
 * @author      shenyc
 * @date        2020-05-06 - ?
 * @copyright   Copy right (c) shenyc (shenyczz@163.com).
 *              All rights reserved.
 *
 ******************************************************************************/
// {{@@@}}
"use strict";
// ----------------------------------------------------------------------------
import type { ExternalOption, GlobalsOption, InputPluginOption, OutputPluginOption, Plugin } from "rollup";
import { externalOption, globalsOption, pluginsCommon, minifyDefault } from "./internal";
// ----------------------------------------------------------------------------
/**
 * RollupConfig 偏好选项
 */
interface PreferenceOption {
    /**
     * 输出路径
     * @defaultValue "dist"
     */
    outDir?: string;
    /**
     * 输出模块基本名称 bundleBaseName
     * @defaultValue "index"
     */
    bundleBaseName?: string;
    /**
     * 外部模块
     */
    externals?: ExternalOption;
    /**
     * 全局选项
     */
    globals?: GlobalsOption;
    /**
     * 压缩插件
     */
    minify?: Plugin;
    /**
     * 插件
     */
    plugins?: InputPluginOption | OutputPluginOption;
    /**
     * banner
     */
    banner?: string;
    /**
     * footer
     */
    footer?: string;
}

// option.outDir
const getOutDir = () => PreferenceOption.defaults.outDir;
const setOutDir = (outDir: string) => (PreferenceOption.defaults.outDir = outDir);

// option.bundleBaseName
const getBundleBaseName = () => PreferenceOption.defaults.bundleBaseName;
const setBundleBaseName = (bundleBaseName: string) => (PreferenceOption.defaults.bundleBaseName = bundleBaseName);

// option.externals
const getExternalOption = () => PreferenceOption.defaults.externals;
const setExternalOption = (options: ExternalOption) => (PreferenceOption.defaults.externals = options);
/**
 * 合并外部模块
 * @param options
 * @returns
 */
const mergeExternalOption = (options: ExternalOption) =>
    Object.assign(PreferenceOption.defaults.externals as object, options);

// option.globals
const getGlobalsOption = () => PreferenceOption.defaults.globals;
const setGlobalsOption = (options: GlobalsOption) => (PreferenceOption.defaults.globals = options);
/**
 * 合并全局选项
 * @param options
 * @returns
 */
const mergeGlobalsOption = (options: GlobalsOption) =>
    Object.assign(PreferenceOption.defaults.globals as object, options);

// option.minify
const getMinify = () => PreferenceOption.defaults.minify;
const setMinify = (minify: Plugin) => (PreferenceOption.defaults.minify = minify);

// option.plugins
const getPlugins = () => PreferenceOption.defaults.plugins;
const setPlugins = (plugins: InputPluginOption | OutputPluginOption) => (PreferenceOption.defaults.plugins = plugins);
/**
 * 合并插件
 * @param plugins
 * @returns
 * fix: 2025-02-16 多次调用会有重复插件
 */
const mergePlugins = (plugins: InputPluginOption | OutputPluginOption) => {
    const result = [...(PreferenceOption.defaults.plugins as []), ...(plugins as [])];
    PreferenceOption.defaults.plugins = result;
};

/**
 * 获取偏好选项
 */
const getPreferenceOption = () => PreferenceOption.defaults;

/**
 * 合并偏好选项
 * @param options
 */
const mergePreferenceOption = (options: PreferenceOption) => {
    Object.assign(PreferenceOption.defaults, options);
};

namespace PreferenceOption {
    //
    export const defaults: PreferenceOption = {
        outDir: "dist",
        bundleBaseName: "index",
        externals: externalOption,
        globals: globalsOption,
        minify: minifyDefault,
        plugins: pluginsCommon,
    };
}

export {
    //
    PreferenceOption,
    getPreferenceOption,
    mergePreferenceOption,
    getOutDir,
    setOutDir,
    getBundleBaseName,
    setBundleBaseName,
    getExternalOption,
    setExternalOption,
    mergeExternalOption,
    getGlobalsOption,
    setGlobalsOption,
    mergeGlobalsOption,
    getMinify,
    setMinify,
    getPlugins,
    setPlugins,
    mergePlugins,
};
// ----------------------------------------------------------------------------
// {{@@@}}
